package com.pkk.cloud.support.gateway.dynamic;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: apollo的动态刷新配置
 * @author: peikunkun
 * @create: 2019-05-29 16:01
 **/
@Slf4j
//@Configuration
public class ZuulPropertiesRefresher /*implements ApplicationContextAware*/ {

 /* public static final String PREFIX_ZUUL = "zuul.";

  private ApplicationContext applicationContext;

  @Autowired
  private RouteLocator routeLocator;

  @ApolloConfigChangeListener
  public void onChange(ConfigChangeEvent changeEvent) {
    boolean zuulPropertiesChanged = false;
    for (String changedKey : changeEvent.changedKeys()) {
      if (changedKey.startsWith(PREFIX_ZUUL)) {
        zuulPropertiesChanged = true;
        break;
      }
    }
    if (zuulPropertiesChanged) {
      refreshZuulProperties(changeEvent);
    }
  }

  private void refreshZuulProperties(ConfigChangeEvent changeEvent) {
    //重新绑定配置
    // @see ConfigurationPropertiesRebinder#onApplicationEvent
    this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));

    // 刷新路由
    //see ZuulServerAutoConfiguration.ZuulRefreshListener#onApplicationEvent
    this.applicationContext.publishEvent(new RoutesRefreshedEvent(routeLocator));
  }

  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }*/
}
